package com.itouchtv.yang.library.element;

import io.appium.java_client.TouchAction;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.PointOption;
import org.openqa.selenium.*;
import org.openqa.selenium.interactions.Actions;

import java.time.Duration;
import java.util.List;

/**
 * @apiNote WebElement的包装类，
 */
public class PageElement {
    private By by = null;
    private WebElement webElement = null;
    private WebElement frame = null;

    public WebElement getFrame() {
        return frame;
    }

    public void setFrame(WebElement frame) {
        this.frame = frame;
    }

    public WebElement getWebElement() {
        return webElement;
    }

    public void setWebElement(WebElement webElement) {
        this.webElement = webElement;
    }

    public By getBy() {
        return by;
    }

    public void setBy(By by) {
        this.by = by;
    }

    public void touchPress(TouchAction touchAction){
        touchAction.press(getPointOption()).release().perform();
    }

    public void touchLongPress(TouchAction touchAction,long seconds){
        touchAction.longPress(getPointOption()).waitAction(WaitOptions.waitOptions(Duration.ofSeconds(seconds))).release().perform();
    }

    public void touchMoveto(TouchAction touchAction,int XoffSet,int YoffSet){
        touchAction.press(getPointOption()).moveTo(PointOption.point(XoffSet,YoffSet)).release().perform();
    }

    public void touchMoveto(TouchAction touchAction,PointOption from,PointOption to){
        touchAction.press(from).moveTo(to).release().perform();
    }


    private PointOption getPointOption(){
        Point point = webElement.getLocation();
        Rectangle rectangle = webElement.getRect();
        return  PointOption.point(point.x+rectangle.width/2,point.y+rectangle.height/2);
    }

    public void click() {
        webElement.click();
    }

    public void mouseClick(Actions actions){
        actions.click(webElement).perform();
    }

    public void mouseDoubleClick(Actions actions){
        actions.doubleClick(webElement);
    }

    public void mouseMoveTo(Actions actions){
        actions.moveToElement(webElement);
    }

    public void submit() {
        webElement.submit();
    }

    public void sendKeys(CharSequence... keysToSend) {
        webElement.sendKeys(keysToSend);
    }

    public void clear() {
        webElement.clear();
    }

    public String getTagName() {
        return webElement.getTagName();
    }

    public String getAttribute(String name) {
        return webElement.getAttribute(name);
    }

    public boolean isSelected() {
        return webElement.isSelected();
    }

    public boolean isEnabled() {
        return webElement.isEnabled();
    }

    public String getText() {
        return webElement.getText();
    }

    public List<WebElement> findElements(By by) {
        return webElement.findElements(by);
    }

    public WebElement findElement(By by) {
        return webElement.findElement(by);
    }

    public boolean isDisplayed() {
        return webElement.isDisplayed();
    }

    public Point getLocation() {
        return webElement.getLocation();
    }

    public Dimension getSize() {
        return webElement.getSize();
    }

    public Rectangle getRect() {
        return webElement.getRect();
    }

    public String getCssValue(String propertyName) {
        return webElement.getCssValue(propertyName);
    }

    public <X> X getScreenshotAs(OutputType<X> target) {
        return webElement.getScreenshotAs(target);
    }
}
